<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2018/11/1
 * Time: 11:45
 */
require_once (ROOTDIR."common/db_utils.php");
require_once (ROOTDIR."common/utils.php");
class UserModel {

    public function __construct() {

    }

    // 获取用户列表
    public function getGradeList() {

        $sql = "select DISTINCT(grade) from users";
        $result = DbUtil::getInstance()->query($sql);
        $gradeList = array();
        if ( count($result) == 0 ) {
            return $gradeList;
        }
        foreach ($result as $row ) {
            array_push($gradeList, urlencode($row['grade']));
        }
        return $gradeList;
    }

    /***
     * 查询用户列表
     * @param null $keyWord  关键字(用户名、姓名或学校)
     * @param int $userType 用户类型 1 - 学生, 2 - 老师, 3 - 学生家长
     * @param int $isInZuoZhu 是否是内部人员
     * @param int $userStatus 在机构的状态 1 - 已试听 2 - 学习中 3 - 已暂停 4 - 已毕业
     * @param int $beginIdx 从第几个用户信息开始返回
     * @param int $cnt 返回数量
     * @return array
     */
    public function getUserList($keyWord, $userType, $grade, $class,
                                $isInZuoZhu, $userStatus,
                                $beginIdx = 0, $cnt = 20,
                                $vendor = null,
                                $beginDate = null, $endDate = null,
                                $createrId = null  ) {
        $keyWordStr = null;
        if ( !empty($keyWord) ) {
            $keyWordStr="%$keyWord%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 添加关键字搜索
        if ( !is_null($keyWordStr)) {
            $sqlWhere = "  where CONCAT(`user_id`, `nick`, `school`) like ? ";
            array_push($args, $keyWordStr);
        }

        if ( $userType > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "user_type");
            array_push($args, $userType);
        }

        if ( !empty($grade) ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "grade");
            array_push($args, $grade);
        }

        // 0 表示未知班级
        if ( $class >= 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "class_no");
            array_push($args, $class);
        }

        if ( $isInZuoZhu > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "is_inzuozhu");
            array_push($args, $isInZuoZhu);
        }

        if ( $userStatus > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "user_status");
            array_push($args, $userStatus);
        }

        if ( !empty($beginDate) > 0 && !empty($endDate) ) {
            if ( empty($sqlWhere) ) {
                $sqlWhere = "  where  `reg_time` between ? and ?";
                array_push($args, $beginDate, $endDate);
            }
            else {
                $sqlWhere = "  and  `reg_time` between ? and ?";
                array_push($args, $beginDate, $endDate);
            }
        }

        if ( !empty($createrId) ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "creater_id");
            array_push($args, $createrId);
        }

        $colNames = array();
        array_push($colNames, 'user_id','nick', 'user_type', 'user_status');
        array_push($colNames, 'is_inzuozhu','defunct');
        array_push($colNames, 'submit','solved');
        array_push($colNames, 'accesstime','access_ip', 'reg_time');
        array_push($colNames, 'school','cur_grade', 'cur_upordown', 'start_grade', 'start_upordown');
        array_push($colNames, 'email','phone', 'creater_id', 'grade', 'class_no');

        $sql = DbUtil::genSelectSql($colNames, "users");
        $sql.=$sqlWhere;
        if ( empty($vendor) && isset(SysConfig::$VEND_FOR) ) {
            $vendor = SysConfig::$VEND_FOR;
        }
        if ( $vendor=='school' ) {
            $sql.="  order by `reg_time` desc ";
        }
        else {
            $sql.="  order by `accesstime` desc ";
        }
        $sql.= " limit ".$beginIdx.", ".$cnt;
        // 特别注意：limit后面的数字不能作为$args
        //array_push($args, $beginIdx, $cnt);
        // 执行数据库查询语句
        $result = DbUtil::getInstance()->query2($sql, $args);
        $allUsers = array();
        if ( count($result) == 0 ) {
            return $allUsers;
        }

        $oneUser=array();
        foreach ($result as $row ) {
            $oneUser['id'] = $row['user_id'];
            $oneUser['name'] = urlencode($row['nick']);
            $oneUser['school'] = urlencode($row['school']);
            $oneUser['status'] = intval($row['user_status']);
            $oneUser['accesstime'] = $row['accesstime'];
            $oneUser['createtime'] = "";
            if ( strlen($row['reg_time']) > 0 ) {
                $oneUser['createtime'] = substr($row['reg_time'], 0, -3);
            }
            $oneUser['access_ip'] = $row['access_ip'];
            $oneUser['available'] = $row['defunct'] == 'N';
            $oneUser['type'] = intval($row['user_type']);
            $oneUser['inzuozhu'] = intval($row['is_inzuozhu']);
            $oneUser['grade'] = intval($row['cur_grade']);
            $oneUser['upordown'] = intval($row['cur_upordown']);
            $oneUser['phone'] = urlencode($row['phone']);
            $oneUser['creater_id'] = urlencode($row['creater_id']);

            $oneUser['gradestr'] = urlencode($row['grade']);
            $oneUser['class'] = intval($row['class_no']);
            array_push($allUsers, $oneUser);
        }
        return $allUsers;
    }

    public function getUserCnt($keyWord, $userType, $grade, $class, $isInZuoZhu, $userStatus) {

        $keyWordStr = null;
        if ( strlen($keyWord) > 0 ) {
            $keyWordStr="%$keyWord%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 添加关键字搜索
        if ( !is_null($keyWordStr)) {
            $sqlWhere = "  where CONCAT(`user_id`, `nick`, `school`) like ? ";
            array_push($args, $keyWordStr);
        }

        if ( $userType > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "user_type");
            array_push($args, $userType);
        }

        if ( !empty($grade) ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "grade");
            array_push($args, $grade);
        }

        // 0 表示未知班级
        if ( $class >= 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "class");
            array_push($args, $class);
        }

        if ( $isInZuoZhu > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "is_inzuozhu");
            array_push($args, $isInZuoZhu);
        }

        if ( $userStatus > 0 ) {
            $sqlWhere = DbUtil::genWhereSql($sqlWhere, "user_status");
            array_push($args, $userStatus);
        }

        $sql= "select count(`user_id`) from users ".$sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function editUser($id, $name, $school, $status, $type,
                             $inzuozhu, $grade, $upordown, $phone,
                             $gradestr, $class, $createrId) {
        $id = strtolower($id);
        $colNames = array();
        $args = array();
        array_push($colNames, 'nick',
            'school', 'user_status', 'user_type',
            'is_inzuozhu', 'cur_grade', 'cur_upordown', 'phone',
            'grade', 'class_no');
        array_push($args, $name, $school, $status,
            $type, $inzuozhu, $grade, $upordown,
            $phone, $gradestr, $class);

        if ( $createrId != null ) {
            array_push($colNames, 'creater_id');
            array_push($args, $createrId);
        }

        $sql = DbUtil::genUpdateSql($colNames, "users");
        $sqlWhere = "";
        $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'user_id');
        array_push($args, $id);
        $sql .= $sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }


    public function addUser($id, $name, $school, $status, $type,
                            $inzuozhu, $grade, $upordown, $phone,
                            $gradestr, $class, $createrId) {
        $id = strtolower($id);
        $colNames = array();
        $args = array();

        array_push($colNames, 'user_id','nick', 'school');
        array_push($args, $id, $name, $school);

        array_push($colNames, 'user_status','user_type', 'is_inzuozhu');
        array_push($args, $status, $type, $inzuozhu);

        array_push($colNames, 'cur_grade','cur_upordown', 'start_grade','start_upordown');
        array_push($args, $grade, $upordown, $grade, $upordown);

        array_push($colNames, 'grade', 'class_no');
        array_push($args, $gradestr, $class);

        if ( $phone != null ) {
            array_push($colNames, 'phone');
            array_push($args, $phone);
        }

        array_push($colNames, 'creater_id');
        array_push($args, $createrId);

        $passwd = pwGen('123456');
        array_push($colNames, 'password','reg_time');
        array_push($args,$passwd, date("Y-m-d h:i:s"));

        $sql = DbUtil::genInsertSql($colNames, "users");
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function registerUser($id, $name, $email, $school, $type, $createrId) {
        $id = strtolower($id);
        $colNames = array();
        $args = array();

        array_push($colNames, 'user_id','nick', 'school', 'email');
        array_push($args, $id, $name, $school, $email);

        array_push($colNames, 'user_status','user_type', 'is_inzuozhu');
        array_push($args, 0, $type, 0);

        array_push($colNames, 'creater_id');
        array_push($args, $createrId);

        array_push($colNames, 'defunct');
        array_push($args, 'Y');


        // here

        $passwd = pwGen('123456');
        array_push($colNames, 'password','reg_time');
        array_push($args,$passwd, date("Y-m-d h:i:s"));

        $sql = DbUtil::genInsertSql($colNames, "users");
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function isExist($id) {
        $id = strtolower($id);
        $sql= "select count(*) from users where `user_id` = ?";
        $result = DbUtil::getInstance()->query($sql, $id);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function isExistMail($email) {
        $sql= "select count(*) from users where `email` = ?";
        $result = DbUtil::getInstance()->query($sql, $email);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function delUser($id, $createrId) {
        $colNames = array();
        $args = array();

        array_push($colNames, 'user_id');
        array_push($args, $id);
        if ( $createrId != null ) {
            array_push($colNames, 'creater_id');
            array_push($args, $createrId);
        }

        $sql= DbUtil::genDelSql($colNames, "users");
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function isAdmin($id) {
        $id = strtolower($id);
        $sql= "select count(*) from privilege where rightstr='administrator' and user_id = ?";
        $result = DbUtil::getInstance()->query($sql, $id);
        return intval($result[0][0]);
    }
}